package com.sharemarking.application.common.service;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sharemarking.application.common.entity.Dict;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Leave;
import com.sharemarking.application.common.repository.DictRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.LeaveRepo;

@Controller
@RequestMapping("function")
public class PhoneLeaveController {
	@Autowired
	private LeaveRepo dao;
	
	@Autowired
	private EmployeeRepo er;
	
	
	
	@Autowired
	private DictRepo dr1;
	
	

	@RequestMapping(value = "findleave")
	@ResponseBody
	public List<Leave> findleave(Leave leave) {
		Integer id=leave.getId();
		List<Leave> list=new ArrayList<Leave>();
		if(id!=null){
			Leave leave1=dao.findOne(id);
			list.add(leave1);
		}else {
			list=(List) dao.findByemployee4_idOrderByUpdateDateDesc(leave.getCreateBy());
		}
		return list;
	}

	@RequestMapping(value = "insertleave")
	@ResponseBody
	public String insertleave(Leave leave) {
		Integer id = leave.getId();
		System.out.println(""+id);
		// 编辑请假表
		if (id != null) {
			Leave leave1 = dao.findOne(id);
			leave1.setStartTime(leave.getStartTime());
			leave1.setEndTime(leave.getEndTime());
			leave1.setReason(leave.getReason());
			leave1.setState(leave1.getState());
			dao.save(leave1);
		} else {
			/* 关联关系
			Dict dict = dr1.findOne(0);
			leave.setDict(dict);*/

			Integer employeeId = leave.getCreateBy();//员工id
			Employee e1 = er.findOne(employeeId);
			leave.setEmployee4(e1);
			
			// 请假表的state字段
			leave.setState("未审核");
			dao.save(leave);
		}

		return "1";

	}

	/*@GetMapping(value = "deleteleave")
	@ResponseBody
	public void deteleleave(Integer id) {
		dao.delete(id);
	}*/

}
